package com.xique.park.controller.web;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.request.web.car.ParkCarMultSearchRequest;
import com.xique.park.bean.request.web.parkcar.ParkCarRequest;
import com.xique.park.bean.vo.web.car.ParkCarMultSearchVo;
import com.xique.park.service.service.IParkCarMultSearchService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 多位多车记录查询controller
 *
 * @author wld
 * @date 2022/3/15 9:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/web/multCar")
public class WebParkCarMultSearchController {

    private final IParkCarMultSearchService parkCarMultSearchService;

    @Log(title = "多位多车记录查询")
    @PreAuthorize(hasPermi = "parking:location:list")
    @PostMapping(value = "/list")
    public AjaxResult list(@RequestBody(required = false) ParkCarRequest request) {
        request = checkParam(request);
        PageResult result = parkCarMultSearchService.selectParkCarMultPageList(request);
        return AjaxResult.success(result);
    }

    @Log(title = "多位多车详情")
    @PreAuthorize(hasPermi = "parking:location-detail:query")
    @PostMapping(value = "/detailList")
    public AjaxResult detailList(@RequestBody ParkCarMultSearchRequest request) {
        if (request.getId() == null) {
            return AjaxResult.error();
        }
        ParkCarMultSearchVo result = parkCarMultSearchService.selectDetailList(request);
        return AjaxResult.success(result);
    }

    private ParkCarRequest checkParam(ParkCarRequest request) {
        if (request == null) {
            request = new ParkCarRequest();
        }
        int pageNum = request.getPageNum() == null ? 1 : request.getPageNum();
        int pageSize = request.getPageSize() == null ? 10 : request.getPageSize();
        if (request.getParkId() == null) {
            Long parkId = SecurityUtils.getParkId();
            if (parkId != null) {
                request.setParkId(parkId);
            }
        }
        request.setPageNum(pageNum);
        request.setPageSize(pageSize);
        return request;
    }

}
